
package com.hc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hc.domain.Employee;
import com.hc.domain.Role;
import com.hc.service.IEmployeeService;
import com.hc.service.IRoleService;
import com.hc.util.MD5Util;
import com.hc.util.OtherUtils;
import com.hc.util.PasswordUtil;

@Controller
@RequestMapping("Employee")
public class EmployeeController extends BaseController {
	@Resource
	private IEmployeeService employeeService;
	@Resource
	private IRoleService roleService;
	/**
	 * 
	 * @Description： 获取员工部门分配页数据
	 * @param req
	 * @param resp
	 * @param index 页数
	 * @param count 单页获取几个
	 * @return
	 * @author:  
	 * @since: 2017年6月23日 下午5:42:04
	 */
	@ResponseBody
	@RequestMapping(value = "/getEmployeeList", method = RequestMethod.POST)
	public JSON getEmployeeList(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "pageNumber",required=false) Integer index, 
			@RequestParam(value = "pageSize",required=false) Integer count,
			@RequestParam(value = "roleId",required=false)Integer roleId,
			@RequestParam(value = "name",required=false)String name,
			@RequestParam(value = "tel",required=false)String tel,
			@RequestParam(value = "status",required=false)Integer status) {
		if(index==null||index<=0){
			index = 1;
		}
		if(count==null||count<=0){
			count = 10;
		}
		int total = employeeService.getEmployeeTotal();
		int pageTotal = total/count;
		if (pageTotal==0){
			pageTotal = 1;
		}
		JSONObject voObj = new JSONObject();
		JSONArray arr = new JSONArray();
		List<Employee> employee = employeeService.findEmployeePages(roleId,name,tel,status,index,count);
		if (employee!=null&&employee.size()>0){
			for (Employee e:employee){
				JSONObject empObj = new JSONObject();
				List<Role> roles = roleService.getRoleByEmployeeId(e.getId());
				empObj.put("id", e.getId());
				empObj.put("realName", e.getRealName());
				empObj.put("sex", e.getSex());
				empObj.put("tel", e.getTel());
				empObj.put("createDate", e.getCreatetime());
				String statusString = "";
				if (e.getStatus()==1){
					statusString = "正常";
				}
				if (e.getStatus()==2){
					statusString = "已离职";
				}
				if (e.getStatus()==3){
					statusString = "请假中";
				}
				empObj.put("status", statusString);
				empObj.put("headImg", isEmpty(e.getHeadImg())?"":e.getHeadImg());
				StringBuffer sb = new StringBuffer();
				if (roles!=null&&roles.size()>0){
					for(Role r:roles){
						sb.append(r.getRole()+" ");
					}
					
				}
				empObj.put("roles", sb==null?"":sb.toString());
				arr.add(empObj);
			}
		}
		voObj.put("userList", arr);
		voObj.put("page", index);
		voObj.put("total", total);
		return voObj;
	}
	/**
	 * 
	 * @Description： 修改员工信息
	 * @param req
	 * @param resp
	 * @param id id
	 * @param userName 员工真实姓名
	 * @param tel 联系电话
	 * @param sex 性别
	 * @return
	 * @author:  
	 * @since: 2017年7月4日 下午3:34:59
	 */
	@ResponseBody
	@RequestMapping(value="modify",method = RequestMethod.POST)
	public JSON modifyEmployee(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "id",required=true) Integer id, 
			@RequestParam(value = "userName",required=true) String userName,
			@RequestParam(value = "tel",required=true)String tel,
			@RequestParam(value = "sex",required=true)String sex){
		Employee e = employeeService.getById(id);
		if (e==null){
			return Message(ERROR, "该员工已被删除", null);
		}
		e.setRealName(userName);
		e.setSex(sex);
		e.setTel(tel);
		employeeService.saveOrUpdate(e);
		return Message(SUCCESS, "操作成功", null);
	}
	/**
	 * 
	 * @Description： 修改密码
	 * @param req
	 * @param resp
	 * @param id 用户id
	 * @param newPass 新密码
	 * @return
	 * @author:  
	 * @since: 2017年7月5日 下午2:58:21
	 */
	@ResponseBody
	@RequestMapping(value="modifyPass",method = RequestMethod.POST)
	public JSON modifyPass(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "id",required=true) Integer id,@RequestParam(value = "newPass",required=true) String newPass ){
		Employee e = employeeService.getById(id);
		if (e==null){
			return Message(ERROR, "该员工不存在", null);
		}
		if(isEmpty(newPass)){
			return Message(ERROR, "请正确输入新密码", null);
		}
		if(OtherUtils.getWordCount(newPass)>11||OtherUtils.getWordCount(newPass)<6){
			return Message(ERROR, "密码应大于6位小于12位", null);
		}
		String salt = MD5Util.generatorKey(e.getId());
		String pass = PasswordUtil.MD5Generator(newPass, salt);
		e.setPass(pass);
		e.setSalt(salt);
		employeeService.saveOrUpdate(e);
		return Message(SUCCESS, "密码修改成功", null);
	}
	
}
